% This program gathers the data of speed (average, range), size and time to
% arrive from all the different experiments independently of choice of
% worms. The data of all individuals is then stored in a final matrix; from
% the final matrix creates graphs for the different measures

%needs the sizeandspeedarray_group.mat and sizeandspeedarray_group.m
%folders
clear all

load ('/Users/anapereira/Dropbox/Matlab/data analyzis group/speed and size scripts/labels/sizeandspeedarray_group.mat');%matrix to store the data

load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp5_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp5_ZC2834_speedanalyzisall.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp6_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp6_ZC2834_speedanalyzisall.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp7_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp7_ZC2834_speedanalyzisall.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp8_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp8_ZC2834_speedanalyzisall.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp9_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp9_ZC2834_speedanalyzisall.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp11_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp11_ZC2834_speedanalyzisall.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp16_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp16_ZC2834_speedanalyzisall.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp30_ZC2834_F1/Size and speed analyzis_all/RRF3_Exp30_ZC2834_speedanalyzisall.mat');
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM7_OP vs PA F1/size and speed analyzis all/MT_STM7_speedanalyzisall.mat')
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM8_OP vs PA F1/size and speed analyzis all/MT_STM8_speedanalyzisall.mat')
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM9_OP vs PA F1/size and speed analyzis all/MT_STM9_speedanalyzisall.mat')
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM10_OP vs PA F1/size and speed analyzis all/MT_STM10_speedanalyzisall.mat')
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM12_OP vs PA F1/size and speed analyzis all/MT_STM12_speedanalyzisall.mat')
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM13_OP vs PA F1/size and speed analyzis all/MT_STM13_speedanalyzisall.mat');
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM14_OP vs PA F1/size and speed analyzis all/MT_STM14_speedanalyzisall.mat');
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM15_OP vs PA F1/size and speed analyzis all/MT_STM15_speedanalyzisall.mat');
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM16_OP vs PA F1/size and speed analyzis all/MT_STM16_speedanalyzisall.mat');
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM17_OP vs PA F1/size and speed analyzis all/MT_STM17_speedanalyzisall.mat');
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM19_OP vs PA F1/size and speed analyzis all/MT_STM19_speedanalyzisall.mat');
%load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/MT_STM21_OP vs PA F1/size and speed analyzis all/MT_STM21_speedanalyzisall.mat');

A1 = RRF3_Exp5_ZC2834_speedanalyzisall;
A2 = RRF3_Exp6_ZC2834_speedanalyzisall;
A3 = RRF3_Exp7_ZC2834_speedanalyzisall;
A4 = RRF3_Exp8_ZC2834_speedanalyzisall;
A5 = RRF3_EXp9_ZC2834_speedanalyzisall;
A6 = RRF3_Exp11_ZC2834_speedanalyzisall;
A7 = RRF3_Exp16_ZC2834_speedanalyzisall;
A8 = RRF3_Exp30_ZC2834_speedanalyzisall;
%A9 = MT_STM7_speedanalyzisall;
%A10 = MT_STM8_speedanalyzisall;
%A11 = MT_STM9_speedanalyzisall;
%A12 = MT_STM10_speedanalyzisall;
%A13 = MT_STM12_speedanalyzisall;
%A14 = MT_STM13_speedanalyzisall;
%A15 = MT_STM14_speedanalyzisall;
%A16 = MT_STM15_speedanalyzisall;
%A17 = MT_STM16_speedanalyzisall;
%A18 = MT_STM17_speedanalyzisall;
%A19 = MT_STM19_speedanalyzisall;
%A20 = MT_STM21_speedanalyzisall;

%concatenate the different matrixes
speed_size_data_multiarray = cat(3, A1, A2, A3, A4, A5, A6, A7, A8); %if want to separate experiments specify here

%extract data to new matrix
nexp = size (speed_size_data_multiarray, 3);

size_speed_joinexperiments_all_groups = [];%if want to separate experiments specify here

speed_size_data_multiarray_all_groups = {};% if separate exeriments separate here
%% Average speed
averagespeedOPtoOP = [];

for i=1:nexp
    avgspeedOPtoOP = speed_size_data_multiarray {2,2,i};
    averagespeedOPtoOP = [averagespeedOPtoOP;avgspeedOPtoOP];
end

sizeandspeedanalyzis_together {2,2} = averagespeedOPtoOP; 

averagespeedOPtoPA = [];

for i=1:nexp
    avgspeedOPtoPA = speed_size_data_multiarray {2,3,i};
    averagespeedOPtoPA = [averagespeedOPtoPA;avgspeedOPtoPA];
end

sizeandspeedanalyzis_together {2,3} = averagespeedOPtoPA; 

averagespeedPAtoOP = [];

for i=1:nexp
    avgspeedPAtoOP = speed_size_data_multiarray {2,4,i};
    averagespeedPAtoOP = [averagespeedPAtoOP;avgspeedPAtoOP];
end

sizeandspeedanalyzis_together {2,4} = averagespeedPAtoOP; 

averagespeedPAtoPA = [];

for i=1:nexp
    avgspeedPAtoPA = speed_size_data_multiarray {2,5,i};
    averagespeedPAtoPA = [averagespeedPAtoPA;avgspeedPAtoPA];
end

sizeandspeedanalyzis_together {2,5} = averagespeedPAtoPA; 

%% Speed Range
speedrangeOPtoOP = [];

for i=1:nexp
    speedrgOPtoOP = speed_size_data_multiarray {2,6,i};
    speedrangeOPtoOP = [speedrangeOPtoOP;speedrgOPtoOP];
end

sizeandspeedanalyzis_together {2,6} = speedrangeOPtoOP; 

speedrangeOPtoPA = [];

for i=1:nexp
    speedrgOPtoPA = speed_size_data_multiarray {2,7,i};
    speedrangeOPtoPA = [speedrangeOPtoPA;speedrgOPtoPA];
end

sizeandspeedanalyzis_together {2,7} = speedrangeOPtoPA; 

speedrangePAtoOP = [];

for i=1:nexp
    speedrgPAtoOP = speed_size_data_multiarray {2,8,i};
    speedrangePAtoOP = [speedrangePAtoOP;speedrgPAtoOP];
end

sizeandspeedanalyzis_together {2,8} = speedrangePAtoOP; 

speedrangePAtoPA = [];

for i=1:nexp
    speedrgPAtoPA = speed_size_data_multiarray {2,9,i};
    speedrangePAtoPA = [speedrangePAtoPA;speedrgPAtoPA];
end

sizeandspeedanalyzis_together {2,9} =  speedrangePAtoPA; 

%% Extract data from size
sizeOPtoOP = [];

for i=1:nexp
    szOPtoOP = speed_size_data_multiarray {2,10,i};
    sizeOPtoOP = [sizeOPtoOP;szOPtoOP];
end

sizeandspeedanalyzis_together {2,10} = sizeOPtoOP; 

sizeOPtoPA = [];

for i=1:nexp
    szOPtoPA = speed_size_data_multiarray {2,11,i};
    sizeOPtoPA = [sizeOPtoPA;szOPtoPA];
end

sizeandspeedanalyzis_together {2,11} = sizeOPtoPA; 

sizePAtoOP = [];

for i=1:nexp
    szPAtoOP = speed_size_data_multiarray {2,12,i};
    sizePAtoOP = [sizePAtoOP;szPAtoOP];
end

sizeandspeedanalyzis_together {2,12} = sizePAtoOP; 

sizePAtoPA = [];

for i=1:nexp
    szPAtoPA = speed_size_data_multiarray {2,13,i};
    sizePAtoPA = [sizePAtoPA;szPAtoPA];
end

sizeandspeedanalyzis_together {2,13} =  sizePAtoPA; 

SizeOPw = []; 

for i=1:nexp
    szOPtoOP = speed_size_data_multiarray {2,10,i};
    szOPtoPA = speed_size_data_multiarray {2,11,i};
    SizeOPw = [SizeOPw;szOPtoOP;szOPtoPA];
end

sizeandspeedanalyzis_together {2,14} =  SizeOPw;

SizePAw = []; 

for i=1:nexp
    szPAtoOP = speed_size_data_multiarray {2,12,i};
    szPAtoPA = speed_size_data_multiarray {2,13,i};
    SizePAw = [SizePAw;szPAtoOP;szPAtoPA];
end

sizeandspeedanalyzis_together {2,15} =  SizePAw;

%% Extracts data of time to arrive

timeOPtoOP = [];

for i=1:nexp
    tmOPtoOP = speed_size_data_multiarray {2,14,i};
    timeOPtoOP = [timeOPtoOP;tmOPtoOP];
end

sizeandspeedanalyzis_together {2,16} = timeOPtoOP; 

timeOPtoPA = [];

for i=1:nexp
    tmOPtoPA = speed_size_data_multiarray {2,15,i};
    timeOPtoPA = [timeOPtoPA;tmOPtoPA];
end

sizeandspeedanalyzis_together {2,17} = timeOPtoPA; 

timePAtoOP = [];

for i=1:nexp
    tmPAtoOP = speed_size_data_multiarray {2,16,i};
    timePAtoOP = [timePAtoOP;tmPAtoOP];
end

sizeandspeedanalyzis_together {2,18} = timePAtoOP; 

timePAtoPA = [];

for i=1:nexp
    tmPAtoPA = speed_size_data_multiarray {2,17,i};
    timePAtoPA = [timePAtoPA;tmPAtoPA];
end

sizeandspeedanalyzis_together {2,19} = timePAtoPA; 

%%analyzis of normality

for j=2:19
    data = sizeandspeedanalyzis_together {2,j,1};
    [H, pValue, W] = swtest(data, 0.05);
    sizeandspeedanalyzis_together {3,j,1} = pValue;
end


for k=2:19
    data = sizeandspeedanalyzis_together {2,k,1};
    media = mean (data);
    sizeandspeedanalyzis_together {4,k,1} = media;
end

for l=2:19
    data = sizeandspeedanalyzis_together {2,l,1};
    desviopadrao = std (data);
    sizeandspeedanalyzis_together {5,l,1} = desviopadrao;
end

for m=2:19
    data = sizeandspeedanalyzis_together {2,m,1};
    numberworms = size (data,1);
    sizeandspeedanalyzis_together {6,m,1} = numberworms;
end

for n = 2:19
    desviopadrao = sizeandspeedanalyzis_together {5,n,1};
    numberworms = sizeandspeedanalyzis_together {6,n,1};
    sem = desviopadrao./ (sqrt(numberworms));
    sizeandspeedanalyzis_together {7,n,1} = sem;
end

for o = 2:19
    data = sizeandspeedanalyzis_together {2,o,1};
    mediana = median (data);
    sizeandspeedanalyzis_together {8,o,1} = mediana;
end

for p = 2:19
    data = sizeandspeedanalyzis_together {2,p,1};
    primeiroq = quantile(data,0.25);
    sizeandspeedanalyzis_together {9,p,1} = primeiroq;
end

for q = 2:19
    data = sizeandspeedanalyzis_together {2,q,1};
    terceiroq = quantile(data,0.75);
    sizeandspeedanalyzis_together {10,q,1} = terceiroq;
end


%% Create box plots for Average Speed
avgindspeedOPOP = sizeandspeedanalyzis_together {2,2,1};
avgindspeedOPPA = sizeandspeedanalyzis_together {2,3,1};
avgindspeedPAOP = sizeandspeedanalyzis_together {2,4,1};
avgindspeedPAPA = sizeandspeedanalyzis_together {2,5,1};

sujeitosOPOP = size (avgindspeedOPOP, 1);
sujeitosOPPA = size (avgindspeedOPPA, 1);
sujeitosPAOP = size (avgindspeedPAOP, 1);
sujeitosPAPA = size (avgindspeedPAPA, 1);

rowsmatrix = [];

if sujeitosOPOP>= sujeitosOPPA
    rowsmatrix = sujeitosOPOP;
elseif sujeitosOPOP<sujeitosOPPA
    rowsmatrix = sujeitosOPPA;
end

if rowsmatrix >=sujeitosPAOP
    rowsmatrix = rowsmatrix;
elseif rowsmatrix <sujeitosPAOP
    rowsmatrix = rowsmatrix;
end

if rowsmatrix >=sujeitosPAPA
    rowsmatrix = rowsmatrix;
elseif rowsmatrix <sujeitosPAPA
    rowsmatrix = rowsmatrix;
end

matrix = NaN (rowsmatrix,4);% create a matrix in the begining full of NaN to guarantee that the matrix have 
% all the same dimensions and can be concatenated

matrix (1:sujeitosOPOP, 1) = avgindspeedOPOP; 
matrix (1:sujeitosOPPA, 2) = avgindspeedOPPA; 
matrix (1:sujeitosPAOP, 3) = avgindspeedPAOP; 
matrix (1:sujeitosPAPA, 4) = avgindspeedPAPA; 

figure
boxplot (matrix, 'Labels',{'OP to OP','OP to PA', 'PA to OP', 'PA to PA'});
ylim ([0,600]);
title ('Average speed');
ylabel ('micro/sec');

%% Create box plots for Speed Range
speedrangeOPOP = sizeandspeedanalyzis_together {2,6,1};
speedrangeOPPA = sizeandspeedanalyzis_together {2,7,1};
speedrangePAOP = sizeandspeedanalyzis_together {2,8,1};
speedrangePAPA = sizeandspeedanalyzis_together {2,9,1};

matrix = NaN (rowsmatrix,4);% create a matrix in the begining full of NaN to guarantee that the matrix have 
% all the same dimensions and can be concatenated

matrix (1:sujeitosOPOP, 1) = speedrangeOPOP; 
matrix (1:sujeitosOPPA, 2) = speedrangeOPPA; 
matrix (1:sujeitosPAOP, 3) = speedrangePAOP; 
matrix (1:sujeitosPAPA, 4) = speedrangePAPA; 

figure
boxplot (matrix, 'Labels',{'OP to OP','OP to PA', 'PA to OP', 'PA to PA'});
ylim ([0,1000]);
title ('Speed range');
ylabel ('micro/sec');

%% Create box plots for Size
sizeOPOP = sizeandspeedanalyzis_together {2,10,1};
sizeOPPA = sizeandspeedanalyzis_together {2,11,1};
sizePAOP = sizeandspeedanalyzis_together {2,12,1};
sizePAPA = sizeandspeedanalyzis_together {2,13,1};

matrix = NaN (rowsmatrix,4);% create a matrix in the begining full of NaN to guarantee that the matrix have 
% all the same dimensions and can be concatenated

matrix (1:sujeitosOPOP, 1) = sizeOPOP; 
matrix (1:sujeitosOPPA, 2) = sizeOPPA; 
matrix (1:sujeitosPAOP, 3) = sizePAOP; 
matrix (1:sujeitosPAPA, 4) = sizePAPA; 

figure
boxplot (matrix, 'Labels',{'OP to OP','OP to PA', 'PA to OP', 'PA to PA'});
ylim ([700,1300]);
title ('Size');
ylabel ('micro/sec');

%% Create box plots for Size of OP and PA worms
sizeOP = sizeandspeedanalyzis_together {2,14,1};
sizePA = sizeandspeedanalyzis_together {2,15,1};

sujeitosOP = sujeitosOPOP + sujeitosOPPA;
sujeitosPA = sujeitosPAOP + sujeitosPAPA;

rowsmatrix2 = [];

if sujeitosOP>= sujeitosPA
    rowsmatrix2 = sujeitosOP;
elseif sujeitosOP<sujeitosPA
    rowsmatrix2 = sujeitosPA;
end


matrix = NaN (rowsmatrix2,2);% create a matrix in the begining full of NaN to guarantee that the matrix have 
% all the same dimensions and can be concatenated

matrix (1:sujeitosOP, 1) = sizeOP; 
matrix (1:sujeitosPA, 2) = sizePA; 
 
figure
boxplot (matrix, 'Labels',{'OPworm','PAworm'});
ylim ([700,1300]);
title ('Size');
ylabel ('micro/sec');

%% Create box plots for time to arrive

timeOPOP = sizeandspeedanalyzis_together {2,16,1};
timeOPPA = sizeandspeedanalyzis_together {2,17,1};
timePAOP = sizeandspeedanalyzis_together {2,18,1};
timePAPA = sizeandspeedanalyzis_together {2,19,1};

matrix = NaN (rowsmatrix,4);% create a matrix in the begining full of NaN to guarantee that the matrix have 
% all the same dimensions and can be concatenated

matrix (1:sujeitosOPOP, 1) = timeOPOP; 
matrix (1:sujeitosOPPA, 2) = timeOPPA; 
matrix (1:sujeitosPAOP, 3) = timePAOP; 
matrix (1:sujeitosPAPA, 4) = timePAPA; 

figure
boxplot (matrix, 'Labels',{'OP to OP','OP to PA', 'PA to OP', 'PA to PA'});
ylim ([0,600]);
title ('Time to arrive');
ylabel ('sec');

size_speed_joinexperiments_just__ZC2834_13_11_2019 = sizeandspeedanalyzis_together;
speed_size_data_multiarray_just__ZC2834_13_11_2019 = speed_size_data_multiarray;